package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class EsDocDemo {
    @Autowired
    IHotelService iHotelService;

    @Test
    public void addDocument() throws IOException {
        Hotel hotel = iHotelService.getById(38812);
        HotelDoc hotelDoc = new HotelDoc(hotel);
        IndexRequest request = new IndexRequest("hotel");
        request.id(hotelDoc.getId()+"");
        String jsonstr = JSON.toJSONString(hotelDoc);
        request.source(jsonstr, XContentType.JSON);
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());
    }

    @Test
     public void getDocument() throws IOException {
        GetRequest request = new GetRequest("hotel", "36934");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String hotelStr = response.getSourceAsString();
        HotelDoc hotelDoc = JSON.parseObject(hotelStr, HotelDoc.class);
        System.out.println(hotelDoc);
    }

    @Test
    public void updateById() throws IOException {
        UpdateRequest request = new UpdateRequest("hotel", "36934");
        request.doc("price","398");
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());


    }

    @Test
    public void deleteById() throws IOException {
        DeleteRequest request = new DeleteRequest("hotel", "38665");

        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());
    }

    @Test
     public void bulkAddDocument() throws IOException {
        List<Hotel> list = iHotelService.list();
        BulkRequest request = new BulkRequest("hotel");
        for (Hotel hotel : list) {
            IndexRequest request1 = new IndexRequest();
            request1.id(hotel.getId()+"");
            HotelDoc hotelDoc = new HotelDoc(hotel);
            request1.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
            request.add(request1);

        }
        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(bulk.status());
    }


    RestHighLevelClient client;

    @BeforeEach
    public void init() {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://120.48.83.4:9200")
        ));
    }

    @AfterEach
    public void close() {
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
